<%--
Created by IntelliJ IDEA.
User: wang
Date: 2021/3/16/016
Time: 18:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/common/headerAndLeft.jsp" %>
<script type="text/javascript" src="${ctx}/statics/js/md5.js"></script>

<div class="right">
    <div class="location">
        <strong>你现在所在的位置是:</strong>
        <span>密码修改页面</span>
    </div>
    <div class="providerAdd">
        <form action="#" method="post" name="passwordUpdateForm" id="passwordUpdateForm">
            <input name="id" type="hidden" value="${sessionScope.user.id}">
            <div class="">
                <label for="oldPassword">旧密码：</label>
                <input type="password" name="oldPassword" id="oldPassword" placeholder="请输入原密码" required/>
                <span id="sp1">*</span>
            </div>
            <div>
                <label for="newPassword">新密码：</label>
                <input type="password" name="userPassword" id="newPassword" placeholder="请输入新密码" required/>
                <span id="sp2">*</span>
            </div>
            <div>
                <label for="reNewPassword">确认新密码：</label>
                <input type="password" name="reNewPassword" id="reNewPassword" placeholder="请输入新确认密码" required/>
                <span id="sp3">*</span>
            </div>
            <div class="providerAddBtn">
                <!--<a href="#">保存</a>-->
                <input type="button" value="保存" id="btn"/>
            </div>
        </form>
    </div>
</div>
</section>
<footer class="footer">
    版权归北大青鸟
</footer>
<script type="text/javascript">
    $(function () {
        $("#btn").click(function () {
            //var user="${sessionScope.user}";
            // alert(user);
            var flag1 = false;
            var flag2 = false;
            var flag3 = false;
            // 纯数字或字母
            var regexPassword1 = /^(([A-Za-z_]{6,12})|([\d]{6,12}))$/;
            // 密码可以为任意的字母，数字，下划线，@，.组成，长度6~16位
            var regexPassword2 = /^[\w*@*.*]{6,12}$/;
            var sessionScopeUserPassword = "${sessionScope.user.userPassword}";
            var oldPassword = $("#oldPassword").val();
            var newPassword = $("#newPassword").val();
            var reNewPassword = $("#reNewPassword").val();

            //旧密码验证
            //alert(hex_md5(oldPassword));
            if (hex_md5(oldPassword) == sessionScopeUserPassword) {
                $("#sp1").html("旧密码正确！").css("color", "green");
                flag1 = true;
            } else {
                $("#sp1").html("旧密码不正确！").css("color", "red");
            }
            //新密码验证
            if (newPassword === "" || newPassword === "null") {
                $("#sp2").html("新密码不能为空或null").css("color", "red");
            } else if (newPassword == oldPassword) {
                $("#sp2").html("新密码不能跟旧密码相同").css("color", "red");
            } else {
                if (newPassword.length < 6 || newPassword.length > 12) {
                    $("#sp2").html("新密码长度为6到12位").css("color", "red");
                } else if (regexPassword1.test(newPassword)) {
                    $("#sp2").html("新密码必须由数字和字符组成").css("color", "red");
                } else if (!(regexPassword2.test(newPassword))) {
                    $("#sp2").html("新密码含有非法字符").css("color", "red");
                } else {
                    $("#sp2").html("新密码符合条件").css("color", "green");
                    flag2 = true;
                }
            }
            //确认密码验证
            if (reNewPassword == "" || reNewPassword == "null") {
                $("#sp3").html("确认密码不能为空或null").css("color", "red");
            } else if (reNewPassword != newPassword) {
                $("#sp3").html("确认密码与新密码两次输入不一致！").css("color", "red");
            } else {
                $("#sp3").html("确认密码符合条件！").css("color", "green");
                flag3 = true;
            }
            if (flag1 && flag2 && flag3) {
                var data = $("#passwordUpdateForm").serialize();
                // alert("data----" + data);
                $.post(
                    ctx + "/user/passwordUpdate",
                    data,
                    function (result) {
                        // alert(result);
                        if (result > 0) {
                            alert("修改成功！请重新登录！");
                            location.href = "${ctx}/user/logout";
                        } else {
                            alert("修改失败！");
                        }
                    }
                );
            }


        });
    });
</script>

</body>
</html>